<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class AdminSellingProfit extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_selling_profit';


    /**
     * User: chenniang
     * Date: 2019-10-11 15:30
     * @param $value
     * @param $data
     * @return void
     * descption:待入帐
     */
    public function getWaitingAttr($value,$data){

        $selling_water = new IndexSellingWater();

        $list = $selling_water->where(['user_id'=>$data['user_id'],'waiting'=>1,'status'=>1])->select()->toArray();

        $cash = 0;

        if(!empty($list)){

            foreach ($list as $v){

                $cash+=$v['extract_money_text'];
            }
        }
        return round($cash,2);
    }
    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取分销列表
     */
    public function profitList($dis,$page=10) {
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_user b' ,'a.user_id = b.id')
            ->leftJoin('longbing_card_user_info c' ,'a.user_id = c.fans_id')
            ->where($dis)
            ->field(['a.*,b.nickName,c.name'])
            ->order('a.total_profit desc')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){
                $v['name'] = !empty($v['name'])?$v['name']:$v['nickName'];
            }
        }
        return $data;
    }


    /**
     * @param $dis
     * @param int $page
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-12-29 10:58
     */
    public function profitListV2($dis,$page=10,$mapor=[]) {
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_user b' ,'a.user_id = b.id')
            ->leftJoin('longbing_card_user_info c' ,'a.user_id = c.fans_id')
            ->where($dis)
            ->where(function ($query) use ($mapor){
                $query->whereOr($mapor);
            })
            ->field(['a.*,b.nickName,c.name'])
            ->order('a.total_profit desc')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;
    }




    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取分销关系列表
     */
    public function relationList($dis,$page = 10) {
        $data = Db::name('longbing_card_user')
            ->alias('a')
            ->leftJoin('longbing_card_user b' ,'a.pid = b.id')
            ->leftJoin('longbing_card_selling_profit c' ,'a.id = c.user_id')
            ->where($dis)
            ->where('a.pid','<>',0)
            ->field(['a.nickName,a.id,a.avatarUrl,a.create_money,a.pid,a.create_time,b.nickName as p_nickName,b.avatarUrl as p_avatarUrl,ifnull(c.total_profit,0) as total_profit,ifnull(c.total_postal,0) as total_postal'])
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 16:17
     * @param $dis
     * @param int $page
     * @return array
     * descption:分销关系查找
     */
    public function relationSelect($dis) {
        $data = Db::name('longbing_card_user')
            ->alias('a')
            ->leftJoin('longbing_card_selling_profit c' ,'a.id = c.user_id')
            ->where($dis)
            ->field(['a.id,a.nickName,a.avatarUrl,a.create_money,a.pid,a.create_time,ifnull(c.total_profit,0) as total_profit,ifnull(c.total_postal,0) as total_postal'])
            ->order('a.id desc')
            ->select()
            ->toArray();
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 15:24
     * @param $dis
     * @return array|\think\Model|null
     * descption:获取分销详情
     */
    public function profitInfo($dis){
       $data = $this->where($dis)->find();
       return !empty($data)?$data->toArray():$data;
    }


    /**
     * User: chenniang
     * Date: 2019-09-09 15:32
     * @param $dis
     * @param $data
     * @return AdminSellingProfit
     * descption:修改分销内容
     */
    public function profitUpdate($dis,$data){
        $res = $this->where($dis)->update($data);
        return $res;
    }





}